package com.x.gzcrm.web.controller;

import com.github.pagehelper.PageInfo;
import com.x.gzcrm.model.CURDResult;
import com.x.gzcrm.model.PageResult;
import com.x.gzcrm.model.Role;
import com.x.gzcrm.service.IRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    IRoleService roleService;

    @RequestMapping(value = "/list")
    public String list(){
        return "role/list";
    }

    @RequestMapping(value = "/add")
    public String add(){
        return "role/add";
    }

    @RequestMapping(value = "/edit")
    public String edit(Model model, Integer roleId){

        //1.根据id 获取角色
        Role role = roleService.findByRoleId(roleId);
        String permissionIdsStrByRoleId = roleService.findPermissionIdsStrByRoleId(roleId);
        //2.往model里存个角色
        model.addAttribute("role",role);
        model.addAttribute("ids",permissionIdsStrByRoleId);
       return "role/edit";
    }


    @RequestMapping("listJson")
    @ResponseBody
    /**
     * page 显示的当前页
     * limit 每次显示多少条
     */
    public PageResult<Role> listJson(Role role, int page, int limit){

        PageResult<Role> result = new PageResult<>();
        result.setData(roleService.findAllRoles());

        return result;
    }

    @RequestMapping("save")
    @ResponseBody
    public CURDResult save(Role role, String ids){
        System.out.println(role);
        System.out.println("权限ID" + ids);
        roleService.save(role,ids);
        return new CURDResult();
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public CURDResult delete(Integer roleId){
        CURDResult result = new CURDResult();
        roleService.delete(roleId);
        return result;
    }

    //测试分页插件

    @RequestMapping("listTest")
    @ResponseBody
    public PageInfo<Role> listTest(int page, int pageSize){
        PageInfo<Role> info = roleService.findPageInfo(page,pageSize);
        return info;
    }
}
